local LB_U = require "packages.lb_kill_utility.utility"

local skel = fk.CreateSkill{
  name = "lb_zero__youzhi",
}

Fk:loadTranslationTable{
  ["lb_zero__youzhi"] = "犹执",
  [":lb_zero__youzhi"] = "出牌阶段限一次，你可以令一名其他角色使用一张手牌，且使用时执行你选择的<a href=':lb_zero__zhuipu'><font color='red'>〖缀圃〗</font></a>已删除的一项。",

  ["#lb_zero__youzhi"] = "犹执：是否令一名其他角色使用一张手牌？",
  ["#lb_zero__youzhi-choice"] = "犹执：选择〖缀圃〗已删除的一项，强化 %src 使用的 %arg",
  ["#lb_zero__youzhi-choose"] = "犹执：你可以为此%arg增加1个目标",

  ["$lb_zero__youzhi1"] = "老席德…",
  ["$lb_zero__youzhi2"] = "你在听着吗？",
}

skel:addEffect("active", {
  anim_type = "support",
  prompt = "#lb_zero__youzhi",
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player and not to_select:isKongcheng()
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    if #table.filter(target:getCardIds("h"), function(id) return 
      #Fk:getCardById(id):getDefaultTarget(target, {bypass_times = true}) > 0 end) == 0 then return end
    local use = room:askToUseRealCard(target, {
      pattern = table.filter(target:getCardIds("h"), function(id) return
      #Fk:getCardById(id):getDefaultTarget(target, {bypass_times = true}) > 0 end),
      skill_name = skel.name,
      extra_data = {
        bypass_times = true,
        extraUse = true,
      },
      cancelable = false,
      skip = true,
    })
    if use then
      room:useCard{
        from = target,
        card = use.card,
        tos = use.tos,
        extraUse = true,
        extra_data = {
          lb_zero__youzhier = player,
        }
      }
    end
  end,
})

skel:addEffect(fk.CardUsing, {
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return data.card and (data.extra_data or {}).lb_zero__youzhier == player and LB_U.isWaned(player, "lb_zero__zhuipu")
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local all_choices = {"targetplus1", "bypasstimes", "damageplus1"}
    local choices = {}
    for _, num in ipairs(LB_U.getWanedEffect(player, "lb_zero__zhuipu")) do
      if not LB_U.isWanedEffect(player, "lb_zero__zhuipu", num) then
        table.insert(choices, all_choices[num])
      end
    end
    if #choices == 0 then return end
    local choice = player.room:askToChoice(player, {
      choices = choices,
      skill_name = skel.name,
      prompt = "#lb_zero__youzhi-choice:"..target.id .. "::" ..data.card:toLogString(),
    })
    if choice == "targetplus1" then
      if #data:getExtraTargets() > 0 and not data.from.dead then
        local tos = player.room:askToChoosePlayers(data.from, {
          min_num = 1,
          max_num = 1,
          targets = data:getExtraTargets(),
          skill_name = skel.name,
          prompt = "#lb_zero__youzhi-choose:::"..data.card:toLogString(),
          cancelable = true,
        })
        if #tos > 0 then
          for _, p in ipairs(tos) do
            data:addTarget(p)
          end
        end
      end
    elseif choice == "bypasstimes" then
      if not data.extraUse then
        data.from:addCardUseHistory(data.card.trueName, -1)
        data.extraUse = true
      end
    elseif choice == "damageplus1" then
      data.extra_data = data.extra_data or {}
      data.extra_data.lb_zero__youzhidamageplus = true
    end
  end,
})

skel:addEffect(fk.DamageInflicted, {
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if data.card == nil or target ~= player then return false end
    local room = player.room
    local card_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
    if not card_event then return false end
    local use = card_event.data
    return use.extra_data and use.extra_data.lb_zero__youzhidamageplus and use.extra_data.lb_zero__youzhidamageplus == true
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
    if not card_event then return false end
    local use = card_event.data
    use.extra_data.lb_zero__youzhidamageplus = {}
    data:changeDamage(1)
  end,
})

return skel
